// pages/item-info/item-info.js

const init = require('../../utils/init')
const api = require('../../utils/api')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalprice:0.00,
    itemType:'',
    card: [{
      title: {
        text: "用户须知",
        size:38,
        color:"#000"
      },
      tag: {
        text: "须知详情 >",
        color:"#54E9C5"
      }
    }],
    moneylist:[{
      name:"租金",
      context:"",
      unit:"元"
    },
    {
      name:"押金（可退）",
      context:"",
      unit:"元"
    }
    ],
    infolist:[{
      name:"",
      text:"0",
      unit:"",
    },{
      name:"",
      text:"0",
      unit:""
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    init.initComponents(this)
    this.getItemInfo(options.itemType)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.faceCheck(this)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  /**
   * 获取支付总额
   */
  getTotalPrice(text,context){
    var rent = "moneylist[0].context"
    var deposit = "moneylist[1].context"
    var totalprice = parseFloat(context)+parseFloat(text)
    this.setData({
      [rent]:text,
      [deposit]:context,
      totalprice:totalprice
    })
    console.log(this.data.moneylist)
  },

  getItemInfo(e){
    this.setData({
      itemType:e,
    })
    api.getItemInfo().then(res=>{
      console.log(res)
      if(res.code===1){
        if(e==='box' && res.info.box){
          this.setData({
            infolist:res.info.box,
          })
        }
        if(e==='phoneBox' && res.info.phoneBox){
          this.setData({
            infolist:res.info.phoneBox,
          })
        }
        if(e==='ring' && res.info.ring){
          this.setData({
            infolist:res.info.ring,
          })
      }
      console.log(this.data.infolist[0])
      var rent = this.data.infolist[0].text;
      var deposit = this.data.infolist[0].context;
      this.getTotalPrice(rent,deposit)
    }
    })
  },
  change(){
    var text = this.itemChooseC.data.totalprice
    console.log(text)
    var context =  this.itemChooseC.data.deposit
   this.getTotalPrice(text,context)
  },
  pay(){

    if(app.globalData.faceflag===false){
      app.faceCheck(this)
      return
    }
    
      
    var times = this.itemChooseC.data.timeval;
    var itemType = this.itemChooseC.getType();
    var wxPayDTO={
        times:times,
        itemType:itemType,
        openId:app.globalData.openid,
        nickname:app.globalData.nickname,
    }
    console.log(wxPayDTO)
    api.wxPay(wxPayDTO).then(res=>{
      if(res.code===1){
        var json = res.info
        console.log(json)
        wx.requestPayment({
          ...json,
          success(res){
            console.log(res)
            wx.showLoading({
              title: '订单支付中…',
            })        
            var timer = setInterval(function () {
              if (app.globalData.openid) {
                clearInterval(timer);
                api.getOrderInfo({
                  openId:app.globalData.openid,
                  orderStatus:['pay']
                }).then(res=>{
                  if(res.code===1){
                    wx.hideLoading()
                    console.log(res)
                    wx.switchTab({
                      url: '/pages/item/item',
                    })
                    // wx.navigateTo({
                    //   url: '/pages/ordershow/ordershow?id='+res.info[0].id,
                    // })
                  }
                })
              }
            }, 1500)
          },fail(res){
            console.log(res)
          }
        })
     
      }
    })
  },

  
})